package com.my.hzb.pro.ctrl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.my.hzb.common.bean.GlobalBean;
import com.my.hzb.common.bean.ResultBean;
import com.my.hzb.common.util.JsonUtil;
import com.my.hzb.entiy.Product;
import com.my.hzb.pro.service.ProductService;

@Controller
@RequestMapping("/pro")
public class ProductCtrl {

	private static final Logger log = LogManager.getLogger(ProductCtrl.class);
	
	@Autowired
	private ProductService productService;
	
	@RequestMapping("/list")
	@ResponseBody
	public ResultBean list(Product pro){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		List<Product> list = productService.find(pro);
		if(null!=list){
			result.setReturnList(new ArrayList<Object>(list));
			result.setReturn(ResultBean.RETURN_SUCCESS_CODE, "ok");
		}
		return result;
	}
	@RequestMapping("/update")
	@ResponseBody
	public ResultBean update(Product pro){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		return result;
	}
	@RequestMapping("/updateStock")
	@ResponseBody
	public ResultBean updateStock(Integer proId,Integer type,Integer stock){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		try{
			Product product = productService.getOne(proId);
			if(null==product){
				result.setRespMsg("商品不存在");
				return result;
			}
			int num = productService.updateStock(proId, type, stock);
			log.info("更新库存的结果是："+num);
			if(num<1){
				return result;
			}
			result.setReturnStr(num+"");
			result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
		}catch(Exception e){
			log.error("修改库存失败",e);
		}
		return result;
	}
	@RequestMapping("/delete")
	@ResponseBody
	public ResultBean delete(Product pro){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		
		return result;
	}
	@RequestMapping("/add")
	@ResponseBody
	public ResultBean add(Product pro){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		if(null==pro.getProName()||null==pro.getProDesc()||null==pro.getStock()||null==pro.getPrice())
		{
			result.setRespMsg("完善信息");
			return result;
		}
		if(0==productService.add(pro)){
			return result;
		}
		result.setReturn(ResultBean.RETURN_SUCCESS_CODE, "OK");
		return result;
	}
	@RequestMapping("/getOne")
	@ResponseBody
	public ResultBean getOne(Product pro){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		if(null==pro.getId())
		{
			result.setRespMsg("完善信息");
			return result;
		}
		Product product = productService.getOne(pro.getId());
		if(null!=product){
			result.setReturnObj(product);
			result.setReturnStr(JsonUtil.g.toJson(product));
			result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
		}
		return result;
	}
	@RequestMapping("/getHot")
	@ResponseBody
	public ResultBean getHot(Product pro){
		ResultBean result = new ResultBean(ResultBean.RETURN_ERROR_CODE,ResultBean.RETURN_ERROR_MSG);
		List<Product> list = productService.find(new Product());
		if(null!=list){
			result.setReturnStr(JsonUtil.g.toJson(list.get(0)));
			result.setReturnObj(list.get(0));
			result.setReturnStr(GlobalBean.Url.order+"toAddOrder");
			result.setRespCode(ResultBean.RETURN_SUCCESS_CODE);
			return result;
		}
		return result;
	}
}
